﻿Imports System.Configuration

Public Class frmQuan

    Public username As String = "BoCau"
    Public SoCongTyToiDaMoiQuan As Integer = 5
    Public SoQuanToiDa As Integer = 20
    Public dt As DataTable
    Private Sub TrangChủToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrangChủToolStripMenuItem.Click
        Me.Close()
    End Sub


    Private Sub frmQuan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dt = QuanBUS.GetAllQuanBUS()
        dgvQuan.DataSource = dt
        SoQuanToiDa = Integer.Parse(AppSettingsWriter.GetValue("SoQuanToiDa"))
        SoCongTyToiDaMoiQuan = Integer.Parse(AppSettingsWriter.GetValue("SoCongTyToiDaMoiQuan"))


    End Sub
    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim quan As QuanDTO = New QuanDTO
        If (dt.Rows.Count >= SoQuanToiDa) Then
            MessageBox.Show("Số quận đã đủ không thể thêm")
        Else
            quan.TenQuan = txtTenQuan.Text
            Try
                quan.SoCongTyToiDaMoiQuan = Integer.Parse(txtSoCongTy.Text)
                Dim kt As Integer = QuanBUS.InsertQuanBUS(quan)
                If (kt = 0) Then
                    MessageBox.Show("Số công ty tối đa mỗi quận là: " & SoCongTyToiDaMoiQuan)
                Else
                    dgvQuan.DataSource = QuanBUS.GetAllQuanBUS()
                End If

            Catch ex As Exception
                MessageBox.Show("Số công ty tối đa mỗi quận là số", "Báo lỗi")
            End Try
        End If

    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click

        If (dgvQuan.SelectedRows.Count > 0) Then

            For i As Integer = 0 To (dgvQuan.SelectedRows.Count - 1)

                Try
                    QuanBUS.DeleteQuanBUS(Integer.Parse(dgvQuan.SelectedRows(i).Cells(0).Value.ToString()))

                Catch ex As Exception
                    MessageBox.Show("Quận bạn muốn xóa đang được tham chiếu tới, hãy xóa chúng trước khi xóa", "Báo lỗi")
                End Try
            Next
            dt = QuanBUS.GetAllQuanBUS()
            dgvQuan.DataSource = dt

        End If


    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim quan As QuanDTO = New QuanDTO
        quan.MaQuan = Integer.Parse(txtMaQuan.Text)
        quan.TenQuan = txtTenQuan.Text
        Try
            quan.SoCongTyToiDaMoiQuan = Integer.Parse(txtSoCongTy.Text)
            QuanBUS.UpdateQuanBUS(quan)
            dgvQuan.DataSource = QuanBUS.GetAllQuanBUS()
        Catch ex As Exception
            MessageBox.Show("Số công ty tối đa là số", "Báo lỗi")
        End Try


    End Sub



    Private Sub dgvQuan_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvQuan.SelectionChanged
        If (dgvQuan.SelectedRows.Count > 0 And dgvQuan.SelectedRows.Count < 2) Then

            txtMaQuan.Text = dgvQuan.SelectedRows(0).Cells(0).Value.ToString()
            txtTenQuan.Text = dgvQuan.SelectedRows(0).Cells(1).Value.ToString()
            txtSoCongTy.Text = dgvQuan.SelectedRows(0).Cells(2).Value.ToString()
        End If
    End Sub
End Class